package com.dengyafei.controller;

import com.dengyafei.annotation.JwtToken;
import com.dengyafei.constant.ResultMessage;
import com.dengyafei.result.RestApiResult;
import com.dengyafei.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 邓亚非
 */
@Slf4j
@RestController
@RequestMapping("/jwt")
public class JwtTokenController {
    /**
     * 获取用户登录信息
     *
     * @return
     */
    @JwtToken
    @PostMapping(value = "/getUserInfo")
    public RestApiResult getUserInfo(HttpServletRequest request) {
        log.info("【获取用户登录信息-接口】:[{}]", "无参");
        try {
            String realToken = JwtUtil.getRealToken(request);
            String userInfo = JwtUtil.getUserInfo(realToken);
            log.info("【获取用户登录信息-接口-成功】:[{}]", ResultMessage.Common.ACQUIRE_USERINFO_SUCCESS);
            return RestApiResult.okMsg(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【获取用户登录信息-接口-失败】:[{}]", ResultMessage.Common.ACQUIRE_USERINFO_FAILURE);
            return RestApiResult.okMsg(ResultMessage.Common.ACQUIRE_USERINFO_FAILURE);
        }
    }
}
